<?php
	header('content-type:text/html;charset=utf-8');
class ArticleAction extends Action {
	public function index(){
		if(isset($_SESSION['uid'])){
			$m=M('Content');
			import('ORG.Util.Page');
			$count=$m->count();
			$page=new Page($count,10);
			$pageshow=$page->show();
			$this->assign('pageshow',$pageshow);
			$data=$m->table('lyolzj_menu as m,lyolzj_content as c')->where('m.id=c.menuid')->field('c.id as id,c.title as title,c.status as status,c.author as author,c.ptime as ptime,c.hits as hits,m.title as col')->order('c.ptime desc')->limit($page->firstRow.','.$page->listRows)->select();
			$this->assign('data',$data);
			$this->display();
		}else{
			$this->redirect('Index/index');
		}
	}

	public function create(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/index');
		}
		$m=M('Content');
		$max=$m->max('id');
		$this->assign('max',$max+1);
		$ordering=M('Menu')->order('id')->getField('title,ordering');
		$this->assign('ordering',$ordering);
		$this->display();
	}
	public function addarticle(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/index');
		}
		if ($this->isPost()){
			$m=M('Content');
			$a=$m->create();
			$a['hit']=0;
			$a['ptime']=date('Y-m-d H:i:s',time());
			if($m->add($a)){
				$this->success('添加成功','index');
			}else{
				$this->error('添加失败');
			}
		}
	}
	
	public function delete(){
		if (!isset($_SESSION['uid'])){
			$this->redirect('Index/index');
		}
		if ($this->isPost()) {
			$arr=$this->_post('menubox');
			$m=M('Content');
			for($i=0;$i<count($arr);$i++){
				$where['id']=$arr[$i];
				$m->where($where)->delete();
			}
			$this->success('删除成功','index');
		}
	}
	public function edit(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/index');
		}
		if($this->isGet()){
			$c=M('Content');	
			$mid=$this->_get('mid');
			$where['id']=$mid;
			$data=$c->where($where)->find();
			$m=M('Menu');
			$ordering=$m->getField('title,ordering');
			$this->assign('ordering',$ordering);
			$this->assign('data',$data);
			$this->display('update');
		}
	}
	public function update(){
		if (!isset($_SESSION['uid'])){
			$this->redirect('Index/index');
		}
		if ($this->isPost()) {
			$arr=$this->_post('menubox');
			$m=M('Content');
			$m->create();
			$m->save() or $this->error('失败');
			$this->success('成功','index');
		}
	}
}
?>